package com.linewow.xhyy.nihaonew.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.linewow.xhyy.nihaonew.R;
import com.linewow.xhyy.nihaonew.base.BaseActivity;
import com.linewow.xhyy.nihaonew.base.BaseInfo;
import com.linewow.xhyy.nihaonew.databinding.ItemTagListBinding;
import com.linewow.xhyy.nihaonew.entity.tag.TagListInfo;
import com.linewow.xhyy.nihaonew.model.HttpModel;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.lang.reflect.Type;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * Created by LXR on 2016/8/30.
 */
public class TagListActivity extends BaseActivity {

    @Bind(R.id.toolbar_back_title)
    TextView toolbarBackTitle;
    @Bind(R.id.toolbar_back_left)
    ImageView toolbarBackLeft;
    @Bind(R.id.toolbar_back_toolbar)
    Toolbar toolbarBackToolbar;
    @Bind(R.id.tag_list_lv)
    ListView tagListLv;
    private String TAG = "TagListActivity";
    private List<TagListInfo> tagListInfos;
    private Type tagListInfoType;
    private String key="msg";


    @Override
    public void initView() {
        super.initView();
        setContentView(R.layout.activity_tag_list);
        ButterKnife.bind(this);
        tagListLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(TagListActivity.this,TagDetailActivity.class);
                intent.putExtra(key,tagListInfos.get(position));
                startActivity(intent);
            }
        });
    }

    @Override
    public void initList() {
        super.initList();
    }

    @Override
    public void initData() {
        super.initData();
        requestInfo();
        tagListInfoType = new TypeToken<BaseInfo<TagListInfo>>() {
        }.getType();
    }

    private void requestInfo() {
        OkHttpUtils.get().url(HttpModel.TOPICTAGLIST)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e(TAG, response);
                        Gson gson = new Gson();
                        BaseInfo<TagListInfo> baseInfo = gson.fromJson(response, tagListInfoType);
                        tagListInfos = baseInfo.data;
                        Log.e(TAG, "list的大小" + tagListInfos.size());
                        TagListAdapter tagListAdapter = new TagListAdapter(tagListInfos);
                        tagListLv.setAdapter(tagListAdapter);
                    }
                });
    }

    @OnClick(R.id.toolbar_back_left)
    public void onClick() {
        finish();
    }
    public class TagListAdapter extends BaseAdapter {
        private List<TagListInfo> list;
        private LayoutInflater inflater;

        public TagListAdapter(List<TagListInfo> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            inflater = (LayoutInflater) parent.getContext().getSystemService(LAYOUT_INFLATER_SERVICE);
            ItemTagListBinding binding = DataBindingUtil.inflate(inflater, R.layout.item_tag_list, parent, false);
            binding.setTagListInfo(list.get(position));
            return binding.getRoot();
        }
    }
}
